package cn.rokka.rbac.web.controller;


import cn.rokka.rbac.anno.RequiredPermission;
import cn.rokka.rbac.domain.Employee;
import cn.rokka.rbac.qo.EmployeeQueryObject;
import cn.rokka.rbac.qo.PageResult;
import cn.rokka.rbac.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;


    @RequestMapping("/list")
    @RequiredPermission(name = "员工查询",expression = "employee:saveOrUpdate")
    public String list(Model model, @ModelAttribute("eqo") EmployeeQueryObject eqo){
        PageResult<Employee> query = employeeService.query(eqo);
        List<Employee> employees = employeeService.listAll();
        model.addAttribute("employees",employees);
        model.addAttribute("query",query);
        return "employee/list";
    }

    @RequestMapping("/delete")
    @RequiredPermission(name = "员工删除",expression = "employee:saveOrUpdate")
    public String delete(Long id){
        employeeService.delete(id);
        return "redirect:/employee/list";
    }

    @RequestMapping("/input")
    @RequiredPermission(name = "员工编辑",expression = "employee:saveOrUpdate")
    public String input(Long id, Model model){
        Employee employee = new Employee();
        if (id != null){
            employee = employeeService.getById(id);
        }
        List<Employee> employees = employeeService.listAll();
        model.addAttribute("employees",employees);
        model.addAttribute("employee",employee);
        return "employee/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name = "员工编辑",expression = "employee:saveOrUpdate")
    public String saveOrUpdate(Employee employee, Model model){
        if (employee.getId() != null) {
            employeeService.update(employee);
        }else{
            employeeService.save(employee);
        }
        return "redirect:/employee/list";
    }
}
